package com.chushouya.product.dao.repository;

import com.general.framework.core.lang.Lists;
import com.general.framework.core.lang.Strings;
import com.general.framework.core.schema.PageResult;
import com.general.framework.data.mybatis.BaseRepository;
import com.chushouya.product.dao.entity.SkuEvaluateItemEntity;
import com.chushouya.product.dao.mapper.SkuEvaluateItemEntityMapper;
import com.chushouya.product.dto.admin.skuevaluate.SkuEvaluateItemQuery;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Objects;

@Repository
public class SkuEvaluateItemRepository extends BaseRepository<SkuEvaluateItemEntityMapper, SkuEvaluateItemEntity, Long> {
    
    public List<SkuEvaluateItemEntity> selectList(SkuEvaluateItemQuery query) {
        Example example = buildExample(query);
        return this.selectByExample(example);
    }

    public PageResult<SkuEvaluateItemEntity> selectPageResult(SkuEvaluateItemQuery query) {
        Example example = this.buildExample(query);
        return this.selectPage(query.getPageRequest(), example);
    }
    
    public SkuEvaluateItemEntity selectOne(SkuEvaluateItemQuery query) {
        Example example = buildExample(query);
        return this.selectFirstOneByExample(example);
    }

    private Example buildExample(SkuEvaluateItemQuery query) {
        Example example = new Example(SkuEvaluateItemEntity.class);
        Example.Criteria criteria = example.createCriteria();
        if (Objects.nonNull(query.getSkuItemId())) {
            criteria.andEqualTo("skuItemId", query.getSkuItemId());
        }
        if (Objects.nonNull(query.getProductId())) {
            criteria.andEqualTo("productId", query.getProductId());
        }
        if (Objects.nonNull(query.getSkuId())) {
            criteria.andEqualTo("skuId", query.getSkuId());
        }
        if (Objects.nonNull(query.getItemId())) {
            criteria.andEqualTo("itemId", query.getItemId());
        }
        if (Strings.isNotEmpty(query.getItemName())) {
            criteria.andEqualTo("itemName", query.getItemName());
        }
        if (Objects.nonNull(query.getSelectType())) {
            criteria.andEqualTo("selectType", query.getSelectType());
        }
        if (Objects.nonNull(query.getStatus())) {
            criteria.andEqualTo("status", query.getStatus());
        }
        if (Objects.nonNull(query.getCreateTimeStart())) {
            criteria.andGreaterThanOrEqualTo("createTime", query.getCreateTimeStart());
        }
        if (Objects.nonNull(query.getCreateTimeEnd())) {
            criteria.andLessThanOrEqualTo("createTime", query.getCreateTimeEnd());
        }
        example.orderBy("sort").asc();
        return example;
    }
    
    public List<SkuEvaluateItemEntity> selectListBySkuId(Long skuId) {
        SkuEvaluateItemQuery query = new SkuEvaluateItemQuery();
        query.setSkuId(skuId);
        return selectList(query);
    }
    
    public List<SkuEvaluateItemEntity> selectListBySkuIds(List<Long> skuIds) {
        if (Lists.isEmpty(skuIds)) {
            return Lists.newArrayList();
        }
        Example example = new Example(SkuEvaluateItemEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("skuId", skuIds);
        example.orderBy("sort").asc();
        return this.selectByExample(example);
    }
    
    public List<SkuEvaluateItemEntity> selectListByProductId(Long productId) {
        SkuEvaluateItemQuery query = new SkuEvaluateItemQuery();
        query.setProductId(productId);
        return selectList(query);
    }
    
    public int deleteByProductId(Long productId) {
        if (Objects.isNull(productId)) {
            return 0;
        }
        Example example = new Example(SkuEvaluateItemEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("productId", productId);
        return this.deleteByExample(example);
    }
    
    public int deleteByProductId(Long productId, Long itemId) {
        if (Objects.isNull(productId) || Objects.isNull(itemId)) {
            return 0;
        }
        Example example = new Example(SkuEvaluateItemEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("productId", productId);
        criteria.andEqualTo("itemId", itemId);
        return this.deleteByExample(example);
    }
}
